﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using System.Xml.Linq;
using ProtoType.DataStorage;
using System.Collections.ObjectModel;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Tasks;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Controls.Maps;
using System.Device.Location;
using Microsoft.Phone.Controls.Maps.Platform;
using ProtoType.ProtoTypeService;
using System.IO.IsolatedStorage;
using System.IO;
using Microsoft.Phone;

namespace ProtoType
{
    public partial class ItemDetails : PhoneApplicationPage
    {
        AppSettings apps = new AppSettings();
        ProtoTypeServiceClient client;
        DataStructureStore dss = new DataStructureStore();
        ObservableCollection<FieldProperties> itemlist;
        ObservableCollection<XElement> controls = new ObservableCollection<XElement>();
        ObservableCollection<XElement> formitems = new ObservableCollection<XElement>();
        FieldProperties fp;

        bool newPageInstance = false;
        string selectedfid = "";
        string selecteditemid = "";
        string DetailLayoutType = "";
        string DetailImageControlbid = "";
        string DetailTitle1Controlbid = "";
        string DetailTitle2Controlbid = "";
        string ImageControlType = "";
        string Title1ControlType = "";
        string Title2ControlType = "";

        public ItemDetails()
        {
            InitializeComponent();
            newPageInstance = true;
            client = new ProtoTypeServiceClient();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (NavigationContext.QueryString.TryGetValue("itemid", out selecteditemid) && NavigationContext.QueryString.TryGetValue("fid", out selectedfid))
            {
                if (newPageInstance)
                {
                    if (NavigationContext.QueryString.TryGetValue("fid", out selectedfid))
                    {
                        if (apps.IsolatedStorageSavingSetting)
                        {
                            LoadLayoutControls(dss.LoadlayoutControls(selectedfid));
                            controls = dss.LoadformControlList(selectedfid);
                            formitems = dss.GetSelectedFormItemDetails(selectedfid);
                            GetSelectedFormItemDetails();
                        }
                        if (apps.WebServiceSavingSetting)
                        {
                            client.LoadlayoutControlsAsync(selectedfid);
                            client.LoadlayoutControlsCompleted += new EventHandler<LoadlayoutControlsCompletedEventArgs>(client_LoadlayoutControlsCompleted);
                        }
                    }
                }
                newPageInstance = false;
                lstitemdetails.SelectedIndex = -1;
            }
        }

        void client_LoadlayoutControlsCompleted(object sender, LoadlayoutControlsCompletedEventArgs e)
        {
            LoadLayoutControls(e.Result);
            client.LoadformControlListAsync(selectedfid);
            client.LoadformControlListCompleted += new EventHandler<LoadformControlListCompletedEventArgs>(client_LoadformControlListCompleted);
        }

        void LoadLayoutControls(ObservableCollection<XElement> formelements)
        {
            foreach (XElement ele in formelements)
            {
                if (ele.Element("detaillayout") != null)
                {
                    DetailLayoutType = ele.Element("detaillayout").Attribute("type").Value;
                    if (DetailLayoutType == "Advanced")
                    {
                        DetailImageControlbid = ele.Element("detaillayout").Element("Imagecontrol").Element("bid").Value;
                        DetailTitle1Controlbid = ele.Element("detaillayout").Element("Titlecontrol").Element("bid").Value;
                        DetailTitle2Controlbid = ele.Element("detaillayout").Element("Desccontrol").Element("bid").Value;
                    }
                }
            }
        }

        void client_LoadformControlListCompleted(object sender, LoadformControlListCompletedEventArgs e)
        {
            controls = e.Result;
            client.GetSelectedFormItemDetailsAsync(selectedfid);
            client.GetSelectedFormItemDetailsCompleted += new EventHandler<GetSelectedFormItemDetailsCompletedEventArgs>(client_GetSelectedFormItemDetailsCompleted);
        }

        void client_GetSelectedFormItemDetailsCompleted(object sender, GetSelectedFormItemDetailsCompletedEventArgs e)
        {
            formitems = e.Result;
            GetSelectedFormItemDetails();
            //items = e.Result.ToObservableCollection();
            //client.LoadformControlListAsync(selectedfid);
            //client.LoadformControlListCompleted += new EventHandler<ServiceReference1.LoadformControlListCompletedEventArgs>(client_LoadformControlListCompleted);
        }

        void GetSelectedFormItemDetails()
        {
            BitmapImage bmp = new BitmapImage();
            if (DetailLayoutType == "Advanced")
            {
                GetItemslist();
                stkpnlAdvanced.Visibility = System.Windows.Visibility.Visible;
                if (fp.Pdetailimage != null)
                {
                    ImageSource imgsrc = dss.LoadFromLocalStorage(fp.Pdetailimage, "Images") as ImageSource;
                    ImgDetail.Source = imgsrc;
                }
                tblocktitle1.Text = fp.Pdetailtitle1;
                tblocktitle2.Text = fp.Pdetailtitle2;
            }
            else
            {
                stkpnlAdvanced.Visibility = System.Windows.Visibility.Collapsed;
            }
            BindtoList();
        }

        private void BindtoList()
        {
            var itemsdetails = (from f in formitems where f.Attribute("id").Value == selecteditemid select f).ToList();
            foreach (var fd in itemsdetails.Elements())
            {
                string controltype = "";

                foreach (var f in controls.Elements("control"))
                {
                    if (f.Element("cname").Value.Replace(" ", "") == fd.Name.LocalName)
                    {
                        controltype = f.Element("ctype").Value;
                    }

                }
                if ((fd.Name.LocalName != Title1ControlType) && (fd.Name.LocalName != Title2ControlType) && (fd.Name.LocalName != ImageControlType))
                {
                    StackPanel stp = new StackPanel();
                    stp.Name = "stkpanel";
                    stp.Orientation = System.Windows.Controls.Orientation.Horizontal;

                    TextBlock tblock = new TextBlock();
                    tblock.Name = "tblock" + fd.Name.LocalName;
                    tblock.Text = fd.Name.LocalName;
                    tblock.Width = 150;

                    if (controltype == "Image")
                    {
                        Image image = new Image();
                        image.Height = 100;
                        image.Width = 100;
                        ImageSource imgsrc = dss.LoadFromLocalStorage(fd.Value, "Images") as ImageSource;
                        //BitmapImage bitimg = new BitmapImage();
                        //bitimg.UriSource = new Uri("/Applications/Data/F2DDFFF4-4E09-4542-8D38-9EFA636E63B9/Data/PlatformData/PhotoChooser-490ec5d7-96e4-4b0b-9061-9656f5e5fa04.jpg");
                        image.Source = imgsrc;
                        stp.Children.Add(tblock);
                        stp.Children.Add(image);
                    }
                    else
                    {
                        TextBlock tblockvalue = new TextBlock();
                        tblockvalue.Name = "tblockPhonevalue" + fd.Value;
                        tblockvalue.Text = fd.Value;
                        tblockvalue.Tag = controltype;
                        stp.Children.Add(tblock);
                        stp.Children.Add(tblockvalue);
                    }


                    lstitemdetails.Items.Add(stp);
                }
            }
        }
      
        private void GetItemslist()
        {
            itemlist = new ObservableCollection<FieldProperties>();

            var itemsel = (from f in formitems where f.Attribute("id").Value == selecteditemid select f);

            foreach (var item in itemsel)
            {
                fp = new FieldProperties();
                foreach (XElement control in controls.Elements("control"))
                {
                    int binding = 0;
                    if (apps.IsolatedStorageSavingSetting)
                    {
                        binding = int.Parse(control.Attribute("id").Value);
                    }
                    if (apps.WebServiceSavingSetting)
                    {
                        binding = int.Parse(control.Element("binding").Value);
                    }
                    string cname = control.Element("cname").Value;
                    cname = cname.Replace(" ", "");
                    string ctype = control.Element("ctype").Value;

                    switch (binding)
                    {
                        case 1:
                            if (item.Element(cname) != null)
                            {
                                if ("P1" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P1" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P1" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 2:
                            if (item.Element(cname) != null)
                            {
                                if ("P2" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P2" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P2" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 3:
                            if (item.Element(cname) != null)
                            {
                                if ("P3" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P3" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P3" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 4:
                            if (item.Element(cname) != null)
                            {
                                if ("P4" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P4" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P4" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }

                            }
                            break;
                        case 5:
                            if (item.Element(cname) != null)
                            {
                                if ("P5" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P5" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P5" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 6:
                            if (item.Element(cname) != null)
                            {
                                if ("P6" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P6" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P6" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 7:
                            if (item.Element(cname) != null)
                            {
                                if ("P7" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P7" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P7" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 8:
                            if (item.Element(cname) != null)
                            {
                                if ("P8" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P8" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P8" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 9:
                            if (item.Element(cname) != null)
                            {
                                if ("P9" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P9" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P9" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 10:
                            if (item.Element(cname) != null)
                            {
                                if ("P10" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P10" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P10" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 11:
                            if (item.Element(cname) != null)
                            {
                                if ("P11" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P11" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P11" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 12:
                            if (item.Element(cname) != null)
                            {
                                if ("P12" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P12" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P12" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 13:
                            if (item.Element(cname) != null)
                            {
                                if ("P13" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P13" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P13" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 14:
                            if (item.Element(cname) != null)
                            {
                                if ("P14" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P14" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P14" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 15:
                            if (item.Element(cname) != null)
                            {
                                if ("P15" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P15" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P15" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 16:
                            if (item.Element(cname) != null)
                            {
                                if ("P16" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P16" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P16" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 17:
                            if (item.Element(cname) != null)
                            {
                                if ("P17" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P17" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P17" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 18:
                            if (item.Element(cname) != null)
                            {
                                if ("P18" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P18" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P18" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 19:
                            if (item.Element(cname) != null)
                            {
                                if ("P19" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P19" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P19" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 20:
                            if (item.Element(cname) != null)
                            {
                                if ("P20" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P20" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P20" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 21:
                            if (item.Element(cname) != null)
                            {
                                if ("P21" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P21" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P21" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 22:
                            if (item.Element(cname) != null)
                            {
                                if ("P22" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P22" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P22" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 23:
                            if (item.Element(cname) != null)
                            {
                                if ("P23" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P23" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P23" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 24:
                            if (item.Element(cname) != null)
                            {
                                if ("P24" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P24" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P24" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 25:
                            if (item.Element(cname) != null)
                            {
                                if ("P25" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P25" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P25" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 26:
                            if (item.Element(cname) != null)
                            {
                                if ("P26" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P26" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P26" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 27:
                            if (item.Element(cname) != null)
                            {
                                if ("P27" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P27" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P27" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 28:
                            if (item.Element(cname) != null)
                            {
                                if ("P28" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P28" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P28" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 29:
                            if (item.Element(cname) != null)
                            {
                                if ("P29" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P29" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P29" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 30:
                            if (item.Element(cname) != null)
                            {
                                if ("P30" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P30" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P30" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 31:
                            if (item.Element(cname) != null)
                            {
                                if ("P31" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P31" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P31" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 32:
                            if (item.Element(cname) != null)
                            {
                                if ("P32" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P32" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P32" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 33:
                            if (item.Element(cname) != null)
                            {
                                if ("P33" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P33" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P33" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 34:
                            if (item.Element(cname) != null)
                            {
                                if ("P34" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P34" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P34" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 35:
                            if (item.Element(cname) != null)
                            {
                                if ("P35" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P35" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P35" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                        case 36:
                            if (item.Element(cname) != null)
                            {
                                if ("P36" == DetailTitle1Controlbid)
                                {
                                    fp.Pdetailtitle1 = item.Element(cname).Value;
                                    Title1ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P36" == DetailTitle2Controlbid)
                                {
                                    fp.Pdetailtitle2 = item.Element(cname).Value;
                                    Title2ControlType = item.Element(cname).Name.LocalName;
                                }
                                if ("P36" == DetailImageControlbid)
                                {
                                    fp.Pdetailimage = item.Element(cname).Value;
                                    ImageControlType = item.Element(cname).Name.LocalName;
                                }
                            }
                            break;
                    }
                }
                itemlist.Add(fp);
            }
        }

        private void lstitemdetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string content = "";
            string typename;
            if (lstitemdetails.SelectedIndex != -1)
            {
                StackPanel item = e.AddedItems[0] as StackPanel;
                UIElementCollection colle = item.Children;
                UIElement ele = colle[1];
                Type t = ele.GetType();
                if (t.Name == "TextBlock")
                {
                    TextBlock tb = ele as TextBlock;
                    content = tb.Text;
                    typename = tb.Tag.ToString();

                    switch (typename)
                    {
                        case "Address":
                            NavigationService.Navigate(new Uri("/AddressMap.xaml?addr=" + content, UriKind.Relative));
                            break;

                        case "Audio":
                            MediaPlayerLauncher AudiomediaPlayerLauncher = new MediaPlayerLauncher();
                            AudiomediaPlayerLauncher.Location = MediaLocationType.Install;
                            AudiomediaPlayerLauncher.Media = new Uri(content, UriKind.Relative);
                            AudiomediaPlayerLauncher.Show();
                            break;

                        case "Email":
                            EmailComposeTask ect = new EmailComposeTask();
                            ect.Body = "Hi";
                            ect.Subject = "Hello";
                            ect.To = content;
                            ect.Show();
                            break;

                        case "Phone":
                            PhoneCallTask pc = new PhoneCallTask();
                            pc.PhoneNumber = content;
                            pc.Show();
                            break;

                        case "Url":
                            WebBrowserTask webBrowserTask = new WebBrowserTask();
                            webBrowserTask.URL = content;
                            webBrowserTask.Show();
                            break;

                        case "Video":
                            MediaPlayerLauncher mediaPlayerLauncher = new MediaPlayerLauncher();
                            mediaPlayerLauncher.Location = MediaLocationType.Install;
                            mediaPlayerLauncher.Media = new Uri(content, UriKind.Relative);
                            mediaPlayerLauncher.Show();
                            break;

                    }
                }
            }
        }
    }
}